//
//  BTC+Transaction+Signer.swift
//  XHWalletUtils
//
//  Created by xinph on 2020/5/20.
//  Copyright © 2020 xinph. All rights reserved.
//

import Foundation

extension BTC: SignerProtocol {
    public func signTX(_ hash: String) -> String {
        guard let `private` = self.privateKey else {
            return ""
        }
        let key = BTCKey(privateKey: BTCDataFromHex(self.privateKey))
        guard let res = key?.signature(forHash: BTCDataFromHex(hash)) else {
            return ""
        }
        return res.toHexString()
    }
    
    public func signTX(_ ts: TransactionConvertible, password: String, from: AddressConvertible) -> Result<SignedResultType> {
        
        let key = BTCKey(privateKey: BTCDataFromHex("f1904d2c471f13a693f75331099ef5ea60b050ac01b09bd2172d90d9841072b9"))
        guard let res = key?.signature(forHash: BTCDataFromHex("f7f76c1201a8d9ad8c70a6a57618bd45808f719b08f0ca3661b4adb90e44436b")) else {
            return .failure(XHError(signerError))
        }
        
        return .success(BTCResult(signedTx: "", txHash: res.toHexString()))
    }
}
